package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysGatewayDao;
import cn.iot.sys.entity.SysGateway;
import cn.iot.sys.service.SysGatewayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysGatewayServiceImpl extends ServiceImpl<SysGatewayDao, SysGateway> implements SysGatewayService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getSysGatewayList(Map<String, Object> map) {
        logger.info("SysGatewayServiceImpl getSysGatewayList map{}",map);
        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        String name = (String) map.get("name");
        QueryWrapper<SysGateway> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.eq("isDel",0);
        queryWrapper.orderByDesc("create_time");
        Page<SysGateway> page = new Page<>(pageNum,pageSize);
        Page<SysGateway> page1 = this.baseMapper.selectPage(page, queryWrapper);
        List<SysGateway> sysGatewayList = page1.getRecords();
        int count = this.baseMapper.selectCount(queryWrapper);
        int pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        map.put("sysGatewayList",sysGatewayList);
        map.put("pageCount",pageCount);
        logger.debug("SysGatewayServiceImpl getSysGatewayList sysGatewayList{}",sysGatewayList);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg addSysGateway(SysGateway sysGateway) {
        logger.info("SysGatewayServiceImpl addSysGateway sysGateway{}",sysGateway);
        sysGateway.setCreateTime(new Date());
        int count = this.baseMapper.insert(sysGateway);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }

    @Override
    public ResultMsg updGatewayById(SysGateway sysGateway) {
        logger.info("SysGatewayServiceImpl updGatewayById sysGateway{}",sysGateway);
        sysGateway.setModifyTime(new Date());
        int count = this.baseMapper.updateById(sysGateway);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }

    @Override
    public ResultMsg delGatewayById(int id) {
        logger.info("SysGatewayServiceImpl delGatewayById id{}",id);
        SysGateway sysGateway = new SysGateway();
        sysGateway.setId(id);
        sysGateway.setIsDel(1);
        int count = this.baseMapper.updateById(sysGateway);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",count);
    }
}
